package com.his.struts.action;

import java.sql.ResultSet;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.his.struts.dao.note.NoteDao;
import com.his.struts.dao.note.NoteImp;
import com.his.struts.dao.search.SearchDao;
import com.his.struts.dao.search.SearchImp;
import com.his.struts.dao.users.UsersDao;
import com.his.struts.dao.users.UsersImp;
import com.his.struts.form.DiseaseForm;
import com.his.struts.form.DoctorForm;
import com.his.struts.form.NoteForm;
import com.his.struts.form.UserForm;
import com.his.struts.util.PageUtil;
import com.his.struts.util.TranCharset;
/**
 * 搜索功能
 * @author liumiaoyue
 *
 */
public class SearchAction extends DispatchAction {
	SearchDao dao = new SearchImp();

	/**
	 * Method getDocInfo
	 * 得到医生信息
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 */
	public ActionForward getDocInfo(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		//得到搜索的医生
		String search = request.getParameter("search");
		//查看医生信息
		String look = request.getParameter("look");
		ActionForward forward = null;
		//判断是否为空并放到作用域
		if (search == null) {
			search = "";
		} else {
			DoctorForm searform = dao.getDocInfo(search);
			request.setAttribute("doc", searform);
			//跳转
			forward = mapping.findForward("searchDoc");
		}
		if (look == null) {
			look = "";
		} else {
				//转换字符编码
				look = TranCharset.TranEncodeTOUTF_8(look);
				DoctorForm dinfo = dao.getDocInfo(look);
				request.setAttribute("dinfo", dinfo);
				//跳转
				forward = mapping.findForward("searchDetail");
		}
		return forward;
	}
	
	/**
	 * Method getUserInfo
	 * 得到用户信息
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 */
	public ActionForward getUserInfo(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		String search = request.getParameter("search");
		//得到用户信息
		UserForm uform = dao.getUserInfo(search);
		request.getSession().setAttribute("user", uform);
		ActionForward forward = mapping.findForward("searchUser");
		return forward;
	}
	/**
	 * Method getUserDetailInfo
	 * 得到用户详细信息
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 */
	public ActionForward getUserDetailInfo(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		int uid = Integer.parseInt(request.getParameter("look"));
		UsersDao udao = new UsersImp();
		//得到所有该用户病例
		List<DiseaseForm> dlist = udao.allDisease(uid);
		request.getSession().setAttribute("dlist", dlist);
		ActionForward forward = mapping.findForward("searchUserDetail");
		return forward;
	}
	/**
	 * Method getDiseaseInfo
	 * 得到用户病例信息
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 */
	public ActionForward getDiseaseInfo(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		int caseid = Integer.parseInt(request.getParameter("look"));
		UsersDao udao = new UsersImp();
		DiseaseForm df = udao.oneDisease(caseid);
		request.getSession().setAttribute("oneDisease", df);
		ActionForward forward = mapping.findForward("searchOneDisease");
		return forward;
	}
	
	/**
	 * Method getAllNote
	 * 得到所有日志信息
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 */
	public ActionForward getAllNote(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		UserForm uform = (UserForm)request.getSession().getAttribute("user");
		int uid = uform.getUid();
		NoteDao ndao = new NoteImp();
		
		String sql = "notebook where u_id = " + uid+" order by u_id desc";
		// 得到分页数据
		ResultSet rs = PageUtil.getPage(sql, 10, request);
		if(rs != null){
			// 封装
			List<NoteForm> shownote = ndao.allNote(rs);
			// 数据放到request中
			request.getSession().setAttribute("allNoteList", shownote);
			
		}
		ActionForward forward = mapping.findForward("searchAllNote");
		return forward;
	}
	/**
	 * Method getOneNote
	 * 得到用户日志信息
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 */
	public ActionForward getOneNote(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		String id=request.getParameter("noteid");
		ActionForward forward=null;
		if(id!=null){
			int nid = Integer.parseInt(id);
			NoteDao ndao = new NoteImp();
			NoteForm nf = ndao.showNote(nid);
			request.getSession().setAttribute("oneNoteInfo", nf);
			forward = mapping.findForward("searchOneNote");
		}
		return forward;
	}
	
}